﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CUILoader
{
    public partial class CUIControl : UserControl
    {
        private ICUI parentCUI;

        public ICUI ParentCUI
        {
            get { return parentCUI; }
            set { parentCUI = value; }
        }

        public CUIControl(ICUI value)
        {
            InitializeComponent();
            parentCUI = value;
        }

        public enum eUIType
        {
            TextBox, ComboBox, RadioButton, CheckBox, Label, MainMenu, PictureBox, MessageBox, ListBox, DateTimePicker, NumericUpDown, ProgressBar, Custom
        }

        private eUIType controlType;

        public eUIType ControlType
        {
            get { return controlType; }
            set
            {
                controlType = value;
                switch (controlType)
                {
                    case eUIType.TextBox:
                        ctrl = new TextBox();
                        ((TextBox)ctrl).Multiline = true;
                        this.ctrl.Text = "C'est un TextBox";
                        break;
                    case eUIType.ComboBox:
                        ctrl = new ComboBox();
                        this.ctrl.Text = "C'est un ComboBox";
                        break;
                    case eUIType.RadioButton:
                        ctrl = new RadioButton();
                        this.ctrl.Text = "C'est un RadioButton";
                        break;
                    case eUIType.CheckBox:
                        ctrl = new CheckBox();
                        this.ctrl.Text = "C'est un CheckBox";
                        break;
                    case eUIType.Label:
                        ctrl = new Label();
                        this.ctrl.Text = "C'est un Label";
                        break;
                    case eUIType.MainMenu:
                        //ctrl = new Menu();
                        ctrl = new Button();
                        this.ctrl.Text = "C'est un MainMenu";
                        break;
                    case eUIType.PictureBox:
                        ctrl = new PictureBox();
                        this.ctrl.Text = "C'est un PictureBox";
                        break;
                    case eUIType.MessageBox:
                        ctrl = new Button();
                        this.ctrl.Text = "C'est un MessageBox";
                        //ctrl = new MessageBox();
                        break;
                    case eUIType.ListBox:
                        ctrl = new ListBox();
                        this.ctrl.Text = "C'est un ListBox";
                        break;
                    case eUIType.DateTimePicker:
                        ctrl = new DateTimePicker();
                        break;
                    case eUIType.NumericUpDown:
                        ctrl = new NumericUpDown();
                        this.ctrl.Text = "C'est un NumericUpDown";
                        break;
                    case eUIType.ProgressBar:
                        ctrl = new ProgressBar();
                        this.ctrl.Text = "C'est un ProgressBar";
                        break;
                    case eUIType.Custom:
                        ctrl = new Button();
                        this.ctrl.Text = "C'est un CustomControl";
                        break;
                    default:
                        break;
                }
            }
        }

        Control ctrl;

        public Control Ctrl
        {
            get { return ctrl; }
            set { ctrl = value; }
        }

        private void calculateEdges()
        {
            recNW = new Rectangle(this.ctrl.Bounds.Left - iConstDistance, this.ctrl.Bounds.Top - iConstDistance, iConstDistance * 2, iConstDistance * 2);
            recNE = new Rectangle(this.ctrl.Bounds.Right - iConstDistance, this.ctrl.Bounds.Top - iConstDistance, iConstDistance * 2, iConstDistance * 2);
            recSE = new Rectangle(this.ctrl.Bounds.Right - iConstDistance, this.ctrl.Bounds.Bottom - iConstDistance, iConstDistance * 2, iConstDistance * 2);
            recSW = new Rectangle(this.ctrl.Bounds.Left - iConstDistance, this.ctrl.Bounds.Bottom - iConstDistance, iConstDistance * 2, iConstDistance * 2);
            recN = new Rectangle(this.ctrl.Bounds.Left + iConstDistance, this.ctrl.Bounds.Top - iConstDistance, this.ctrl.Bounds.Width - iConstDistance * 2, iConstDistance * 2);
            recE = new Rectangle(this.ctrl.Bounds.Right - iConstDistance, this.ctrl.Bounds.Top + iConstDistance, iConstDistance * 2, this.ctrl.Bounds.Height - iConstDistance * 2);
            recS = new Rectangle(this.ctrl.Bounds.Left + iConstDistance, this.ctrl.Bounds.Bottom - iConstDistance, this.ctrl.Bounds.Width - iConstDistance * 2, iConstDistance * 2);
            recW = new Rectangle(this.ctrl.Bounds.Left - iConstDistance, this.ctrl.Bounds.Top + iConstDistance, iConstDistance * 2, this.ctrl.Bounds.Height - iConstDistance * 2);
        }

        public void initCtrl()
        {
            this.ctrl.Name = "Control";
            //this.ctrl.Text = "Control";
            this.ctrl.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ctrl_MouseMove);
            this.ctrl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ctrl_MouseDown);
            this.ctrl.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ctrl_MouseUp);

            this.Controls.Add(this.ctrl);
            this.ctrl.Bounds = this.Bounds;
            calculateEdges();
        }
        private int iConstDistance = 6;
        Rectangle recNW, recNE, recSE, recSW, recN, recE, recS, recW;

        public enum ePosition
        {
            North, West, South, East, NorthWest, NorthEast, SouthWest, SouthEast, Inside
        }

        public ePosition isAtEdges(int iX, int iY)
        {
            Point p = new Point(iX, iY);
            if (recNW.Contains(p))
                return ePosition.NorthWest;
            if (recNE.Contains(p))
                return ePosition.NorthEast;
            if (recSE.Contains(p))
                return ePosition.SouthEast;
            if (recSW.Contains(p))
                return ePosition.SouthWest;
            if (recN.Contains(p))
                return ePosition.North;
            if (recS.Contains(p))
                return ePosition.South;
            if (recW.Contains(p))
                return ePosition.West;
            if (recE.Contains(p))
                return ePosition.East;
            return ePosition.Inside;
        }

        bool bIsResizable = false;
        private ePosition eClickedPosition = ePosition.Inside;

        public ePosition EClickedPosition
        {
            get { return eClickedPosition; }
            set { eClickedPosition = value; }
        }

        private Point pOriginClick;

        private void ctrl_MouseDown(object sender, MouseEventArgs e)
        {
            eClickedPosition = isAtEdges(e.X, e.Y);
            if (e.Button == MouseButtons.Left)
                pOriginClick = e.Location;
            bIsResizable = true;

            this.OnMouseDown(e);
        }

        private void ctrl_MouseMove(object sender, MouseEventArgs e)
        {
            if (isAtEdges(e.X, e.Y) == ePosition.NorthWest || isAtEdges(e.X, e.Y) == ePosition.SouthEast)
                this.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
            else if (isAtEdges(e.X, e.Y) == ePosition.NorthEast || isAtEdges(e.X, e.Y) == ePosition.SouthWest)
                this.Cursor = System.Windows.Forms.Cursors.SizeNESW;
            else if (isAtEdges(e.X, e.Y) == ePosition.North || isAtEdges(e.X, e.Y) == ePosition.South)
                this.Cursor = System.Windows.Forms.Cursors.SizeNS;
            else if (isAtEdges(e.X, e.Y) == ePosition.West || isAtEdges(e.X, e.Y) == ePosition.East)
                this.Cursor = System.Windows.Forms.Cursors.SizeWE;
            else
                this.Cursor = System.Windows.Forms.Cursors.SizeAll;

            if (bIsResizable)
            {
                if (this.eClickedPosition == CUIControl.ePosition.NorthWest)
                {
                    this.Location = new Point(this.Left + (e.X - pOriginClick.X), this.Top + (e.Y - pOriginClick.Y));
                    this.Width = this.Width + (pOriginClick.X - e.X);
                    this.Height = this.Height + (pOriginClick.Y - e.Y);
                }
                else if (this.eClickedPosition == CUIControl.ePosition.SouthEast)
                {
                    this.Width = e.X;
                    this.Height = e.Y;
                }
                else if (this.eClickedPosition == CUIControl.ePosition.SouthWest)
                {
                    this.Location = new Point(this.Left += (e.X - pOriginClick.X), this.Top);
                    this.Width = this.Width + (pOriginClick.X - e.X);
                    this.Height = e.Y;
                }
                else if (this.eClickedPosition == CUIControl.ePosition.NorthEast)
                {
                    this.Location = new Point(this.Left, this.Top + (e.Y - pOriginClick.Y));
                    this.Width = e.X;
                    this.Height += (pOriginClick.Y - e.Y);
                }
                else if (this.eClickedPosition == CUIControl.ePosition.North)
                {
                    this.Location = new Point(this.Left, this.Top + (e.Y - pOriginClick.Y));
                    this.Height += (pOriginClick.Y - e.Y);
                }
                else if (this.eClickedPosition == CUIControl.ePosition.South)
                {
                    this.Height = e.Y;
                }
                else if (this.eClickedPosition == CUIControl.ePosition.East)
                {
                    this.Width = e.X;
                }
                else if (this.eClickedPosition == CUIControl.ePosition.West)
                {
                    this.Location = new Point(this.Left + (e.X - pOriginClick.X), this.Top);
                    this.Width += (pOriginClick.X - e.X);
                }
                else if (this.eClickedPosition == CUIControl.ePosition.Inside)
                {
                    this.Location = new Point(this.Left + (e.X - pOriginClick.X), this.Top + (e.Y - pOriginClick.Y));
                }
                this.ctrl.Width = this.Width;
                this.ctrl.Height = this.Height;
                calculateEdges();
            }

        }

        private void ctrl_MouseUp(object sender, MouseEventArgs e)
        {
            bIsResizable = false;
        }
    }
}
